﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;

namespace AnyOffice.general.person_info
{
    public partial class info : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table = new UsersDao().SelectByUserId(base.Conn, base.User.Identity.Name);
                if (table.Rows.Count > 0)
                {
                    this.txtUserName.Text = (table.Rows[0]["UserName"] != DBNull.Value) ? table.Rows[0]["UserName"].ToString() : "";
                    this.ddlSex.Items.FindByValue(table.Rows[0]["Sex"].ToString()).Selected = true;
                    DateTime time = Convert.ToDateTime(table.Rows[0]["Birthday"].ToString());
                    this.ddlBirthday_Year.Items.FindByValue(time.Year.ToString()).Selected = true;
                    this.ddlBirthday_Mon.Items.FindByValue(time.Month.ToString()).Selected = true;
                    this.ddlBirthday_Day.Items.FindByValue(time.Day.ToString()).Selected = true;
                    if (Convert.ToBoolean(table.Rows[0]["IsHideBirthday"].ToString()))
                    {
                        this.ckbIsHideBirthday.Checked = true;
                    }
                    this.txtTelNoDept.Text = (table.Rows[0]["TelNoDept"] != DBNull.Value) ? table.Rows[0]["TelNoDept"].ToString() : "";
                    this.txtFaxNoDept.Text = (table.Rows[0]["FaxNoDept"] != DBNull.Value) ? table.Rows[0]["FaxNoDept"].ToString() : "";
                    this.txtAddressHome.Text = (table.Rows[0]["AddressHome"] != DBNull.Value) ? table.Rows[0]["AddressHome"].ToString() : "";
                    this.txtZipHome.Text = (table.Rows[0]["ZipHome"] != DBNull.Value) ? table.Rows[0]["ZipHome"].ToString() : "";
                    this.txtTelHome.Text = (table.Rows[0]["TelHome"] != DBNull.Value) ? table.Rows[0]["TelHome"].ToString() : "";
                    this.txtMobile.Text = (table.Rows[0]["Mobile"] != DBNull.Value) ? table.Rows[0]["Mobile"].ToString() : "";
                    if (Convert.ToBoolean(table.Rows[0]["IsHideMobile"].ToString()))
                    {
                        this.ckbIsHideMobile.Checked = true;
                    }
                    this.txtBp.Text = (table.Rows[0]["Bp"] != DBNull.Value) ? table.Rows[0]["Bp"].ToString() : "";
                    this.txtEmail.Text = (table.Rows[0]["Email"] != DBNull.Value) ? table.Rows[0]["Email"].ToString() : "";
                    this.txtOicq.Text = (table.Rows[0]["Oicq"] != DBNull.Value) ? table.Rows[0]["Oicq"].ToString() : "";
                    this.txtIcq.Text = (table.Rows[0]["Icq"] != DBNull.Value) ? table.Rows[0]["Icq"].ToString() : "";
                    this.txtMsn.Text = (table.Rows[0]["Msn"] != DBNull.Value) ? table.Rows[0]["Msn"].ToString() : "";
                    this.txtSkype.Text = (table.Rows[0]["Skype"] != DBNull.Value) ? table.Rows[0]["Skype"].ToString() : "";
                }
                StringBuilder builder = new StringBuilder("<script>\n");
                builder.Append("CheckDate(1);\n");
                builder.Append("</script>\n");
                if (!this.Page.IsStartupScriptRegistered("CheckDate"))
                {
                    this.Page.RegisterStartupScript("CheckDate", builder.ToString());
                }
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                UsersDao dao2 = new UsersDao();
                UsersData data = new UsersData();
                data.UserId = base.User.Identity.Name;
                data.Sex = Convert.ToBoolean(this.ddlSex.SelectedValue);
                data.Birthday = Convert.ToDateTime(this.ddlBirthday_Year.Value + "-" + this.ddlBirthday_Mon.Value + "-" + this.ddlBirthday_Day.Value);
                data.IsHideBirthday = this.ckbIsHideBirthday.Checked;
                if (this.txtTelNoDept.Text != string.Empty)
                {
                    data.TelNoDept = this.txtTelNoDept.Text;
                }
                if (this.txtFaxNoDept.Text != string.Empty)
                {
                    data.FaxNoDept = this.txtFaxNoDept.Text;
                }
                if (this.txtAddressHome.Text != string.Empty)
                {
                    data.AddressHome = this.txtAddressHome.Text;
                }
                if (this.txtZipHome.Text != string.Empty)
                {
                    data.ZipHome = this.txtZipHome.Text;
                }
                if (this.txtTelHome.Text != string.Empty)
                {
                    data.TelHome = this.txtTelHome.Text;
                }
                if (this.txtMobile.Text != string.Empty)
                {
                    data.Mobile = this.txtMobile.Text;
                }
                data.IsHideMobile = this.ckbIsHideMobile.Checked;
                if (this.txtBp.Text != string.Empty)
                {
                    data.Bp = this.txtBp.Text;
                }
                if (this.txtEmail.Text != string.Empty)
                {
                    data.Email = this.txtEmail.Text;
                }
                if (this.txtOicq.Text != string.Empty)
                {
                    data.Oicq = this.txtOicq.Text;
                }
                if (this.txtIcq.Text != string.Empty)
                {
                    data.Icq = this.txtIcq.Text;
                }
                if (this.txtMsn.Text != string.Empty)
                {
                    data.Msn = this.txtMsn.Text;
                }
                if (this.txtSkype.Text != string.Empty)
                {
                    data.Skype = this.txtSkype.Text;
                }
                dao2.UpdatePersonal(base.Conn, data);
                this.lblMessage.Text = base.ShowPrompt("已保存修改", "index.aspx");
            }
        } 

    }
}
